const GitServer = require("./GitServer");
const GiteeRequest = require("./GiteeRequest");

class Gitee extends GitServer {
  constructor() {
    super("gitee");
    this.request = null;
  }

  setToken(token) {
    super.setToken(token);
    this.request = new GiteeRequest(token);
  }

  getUser() {
    return this.request.get("/user");
  }

  /**
   * 获取组织列表
   * @param username
   * @returns {*}
   */
  getOrg(username) {
    return this.request.get(`/users/${username}/orgs`, {
      page: 1,
      per_page: 100,
    });
  }

  getRepo(login, name) {
    return new Promise((resolve, reject) => {
      this.request
        .get(`/repos/${login}/${name}`)
        .then((response) => {
          resolve(this.handleResponse(response));
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  createRepo(name) {
    return this.request.post("/user/repos", {
      name,
    });
  }

  createOrgRepo(name, login) {
    return this.request.post(`/orgs/${login}/repos`, {
      name,
    });
  }

  getTokenUrl() {
    return "https://gitee.com/personal_access_tokens";
  }

  getTokenHelpUrl() {
    return "https://gitee.com/help/articles/4191";
  }

  // 注意；这里因为用的是gitee/github的ssh操作，因此仓库源地址应该是ssh方式的地址而不是https的地址
  getRemote(login, name) {
    return `git@gitee.com:${login}/${name}.git`;
  }
}

module.exports = Gitee;
